function showDiv(elementId) {
	var mask = document.getElementById('mask');
	mask.style.display = "";
	var element = document.getElementById(elementId);
	element.style.display = "";
}
function hiddenDiv(elementId) {
	var mask = document.getElementById('mask');
	mask.style.display = "none";
	var element = document.getElementById(elementId);
	element.style.display = "none";
}
function sign(id) {
	var empIds = document.getElementsByName('empId');
	for ( var i = 0; i < empIds.length; i++) {
		var empId = empIds.item(i);
		if (empId.value == id) {
			empId.checked = true;
			break;
		}
	}
}
function insertButton() {
	clearValue();
	showDiv('sus');
}
function updateButton() {
	var empIds = document.getElementsByName('empId');
	var id = 0;
	for ( var i = 0; i < empIds.length; i++) {
		var empId = empIds.item(i);
		if (empId.checked == true) {
			id = empId.value;
			break;
		}
	}
	update(id);
}
function deleteButton() {
	deleteEmp();
}
function selectButton() {
	clearValue();
	showDiv('sus');
	document.getElementById("add").style.display = "none";
	document.getElementById("modify").style.display = "none";
	document.getElementById("find").style.display = "";
}
function update(id, trid) {
	document.getElementById("add").style.display = "none";
	document.getElementById("modify").style.display = "";
	document.getElementById("find").style.display = "none";
	if (id != 0) {
		showDiv('sus');
	} else {
		alert("请选择员工");
	}
	var trElement = document.getElementById('' + trid);
	var str = '';
	var tds = trElement.getElementsByTagName('td');
	var temp = '';
	var empId = tds.item(0).getAttribute('empId');
	var permission = tds.item(0).getAttribute('permission');
	var employmentId = tds.item(0).getAttribute('employmentId');
	var isOper = tds.item(0).getAttribute('isOper');

	if (empId == id) {
		temp = temp + empId + '.' + permission + '.' + employmentId + '.'
				+ isOper;
		for ( var j = 1; j < tds.length; j++) {
			var td = tds.item(j);
			temp = temp + '.' + td.innerText;
		}
	}
	str = str + temp;
	var strArr = str.split('.');
	setValue(strArr);
}
function clearValue() {
	document.getElementById("add").style.display = "";
	document.getElementById("modify").style.display = "none";
	document.getElementById("find").style.display = "none";
	document.all.employeeId.value = "";
	document.all.employeeName.value = "";
	document.all.empEmplId.value = "";
	document.all.employeeTel.value = "";
	document.all.employeeAdd.value = "";
	document.all.employeeMemo.value = "";
	document.getElementById("isOper").checked = false;
	var pers = document.getElementsByName('employeePermission');
	for ( var i = 0; i < pers.length; i++) {
		pers.item(i).checked = false;
	}
}
function setValue(strArr) {
	document.all.employeeId.value = strArr[0];
	var pers = document.getElementsByName('employeePermission');
	for ( var i = 0; i < pers.length; i++) {
		var val = pers.item(i).value;
		var values = strArr[1].split(',');
		for ( var j = 0; j < values.length; j++) {
			if (val == values[j]) {
				pers.item(i).checked = true;
				break;
			} else {
				pers.item(i).checked = false;
			}
		}
	}
	if (strArr[3] == 1) {
		document.getElementById("isOper").checked = true;
	} else {
		document.getElementById("isOper").checked = false;
	}
	document.all.empEmplId.value = strArr[2];
	document.all.employeeName.value = strArr[4];
	document.all.employeeTel.value = strArr[6];
	document.all.employeeAdd.value = strArr[7];
	document.all.employeeMemo.value = strArr[8];

}
function callback(data) {
	var isOperBox = document.getElementById("isOper");
	isOperBox.checked = false;
	if ("" != data.lenght) {
		hiddenDiv('sus');
	}
	var str = "";
	for ( var i = 0; i < data.length; i++) {
		// 动态的改变页面中div节点中的内容
		str = str + '<tr id="tr' + i + '" ondblclick="update('
				+ data[i].employeeId + ',\'tr' + i + '\');" onclick="sign('
				+ data[i].employeeId + ');">' + '<td width="20" empId="'
				+ data[i].employeeId + '" permission="'
				+ data[i].employeePermission + '" employmentId="'
				+ data[i].empEmplId + '" isOper="' + data[i].isOper
				+ '" ><input type="radio" name="empId"' + 'isOper='
				+ data[i].isOper + ' value="' + data[i].employeeId
				+ '" /></td>' + '<td width="100" >' + data[i].employeeName
				+ '</td>' + '<td width="70" >' + data[i].empEmplName + '</td>'
				+ '<td width="80" >' + data[i].employeeTel + '</td>'
				+ '<td width="200" >' + data[i].employeeAdd + '</td>'
				+ '<td width="92" >' + data[i].employeeMemo + '</td>' + '</tr>';
	}
	clearValue();
	$("#empList").html(str);
}
// 添加 员工 部分
function createEmp() {
	var employeeName = escape(escape($("#employeeName").val()));
	var empEmplId = escape(escape($("#empEmplId").val()));
	var employeeTel = escape(escape($("#employeeTel").val()));
	var employeeAdd = escape(escape($("#employeeAdd").val()));
	var employeeMemo = escape(escape($("#employeeMemo").val()));
	var permissionBox = document.getElementsByName("employeePermission");
	var isOperBox = document.getElementById("isOper");
	var isOper = 0;
	if (isOperBox.checked == true) {
		isOper = 1;
	}
	var employeePermission = new Array();
	var j = 0;
	for ( var i = 0; i < permissionBox.length; i++) {
		if (permissionBox[i].checked) {
			employeePermission[j] = escape(escape(permissionBox[i].value));
			j++;
		}
	}
	var url = '/stock/pages/system/employee.do?method=createEmployee&d=' + new Date()
			+ '&employeeName=' + employeeName + '&empEmplId=' + empEmplId
			+ '&employeeTel=' + employeeTel + '&employeeAdd=' + employeeAdd
			+ '&employeeMemo=' + employeeMemo + '&employeePermission='
			+ employeePermission + '&isOper=' + isOper;
	$.getJSON(url, {}, callback);
}
// 修改 员工 部分
function updateEmp() {
	var employeeId = $("#employeeId").val();
	var employeeName = escape(escape($("#employeeName").val()));
	var empEmplId = escape(escape($("#empEmplId").val()));
	var employeeTel = escape(escape($("#employeeTel").val()));
	var employeeAdd = escape(escape($("#employeeAdd").val()));
	var employeeMemo = escape(escape($("#employeeMemo").val()));
	var permissionBox = document.getElementsByName("employeePermission");
	var isOperBox = document.getElementById("isOper");
	var isOper = 0;
	if (isOperBox.checked == true) {
		isOper = 1;
	}
	var employeePermission = new Array();
	var j = 0;
	for ( var i = 0; i < permissionBox.length; i++) {
		if (permissionBox[i].checked) {
			employeePermission[j] = escape(escape(permissionBox[i].value));
			j++;
		}
	}
	var url = '/stock/pages/system/employee.do?method=updateEmployee&d=' + new Date()
			+ '&employeeId=' + employeeId + '&employeeName=' + employeeName
			+ '&empEmplId=' + empEmplId + '&employeeTel=' + employeeTel
			+ '&employeeAdd=' + employeeAdd + '&employeeMemo=' + employeeMemo
			+ '&employeePermission=' + employeePermission + '&isOper=' + isOper;
	$.getJSON(url, {}, callback);
}
// 删除 员工 部分
function deleteEmp() {
	if (confirm("真的要删除吗？删除后将无法恢复！")) {
		var empIdBox = document.getElementsByName("empId");
		var empId = 0;
		var isOper = 0;
		for ( var i = 0; i < empIdBox.length; i++) {
			if (empIdBox[i].checked) {
				isOper = empIdBox[i].isOper;
				empId = empIdBox[i].value;
				break;
			}
		}
		var url = '/stock/pages/system/employee.do?method=deleteEmployee&d='
				+ new Date() + '&employeeId=' + empId + '&isOper=' + isOper;
		$.getJSON(url, {}, callback);
	}
}
// 查找 员工 部分
function selectEmp() {
	var employeeId = $("#employeeId").val();
	var employeeName = escape(escape($("#employeeName").val()));
	var empEmplId = escape(escape($("#empEmplId").val()));
	var employeeTel = escape(escape($("#employeeTel").val()));
	var employeeAdd = escape(escape($("#employeeAdd").val()));
	var employeeMemo = escape(escape($("#employeeMemo").val()));
	var permissionBox = document.getElementsByName("employeePermission");
	var employeePermission = new Array();
	var j = 0;
	for ( var i = 0; i < permissionBox.length; i++) {
		if (permissionBox[i].checked) {
			employeePermission[j] = escape(escape(permissionBox[i].value));
			j++;
		}
	}
	var url = '/stock/pages/system/employee.do?method=selectEmployee&d=' + new Date()
			+ '&employeeId=' + employeeId + '&employeeName=' + employeeName
			+ '&empEmplId=' + empEmplId + '&employeeTel=' + employeeTel
			+ '&employeeAdd=' + employeeAdd + '&employeeMemo=' + employeeMemo
			+ '&employeePermission=' + employeePermission;
	$.getJSON(url, {}, callback);
}
function selectAllEmp() {
	var url = '/stock/pages/system/employee.do?method=selectEmployee&d=' + new Date();
	$.getJSON(url, {}, callback);
}